import UICore from "../../../../Core/Res/UICore";
import * as fgui from "fairygui-cc";
import AbstractUIWindow from "../../window/AbstractUIWindow";
import AudioManager from "../../../../Core/Audio/AudioManager";
import SdkTool from "../../../../Core/Sdk/SdkTool";
import { GameModels } from "../../socket/GameModels";
import { LocalUtil } from "../../../../Core/Utils/LocalUtil";
import EventMgr from "../../../../Core/Event/EventMgr";
import { support } from "../../../proto/protoCmd";
import { DateUtils } from "../../../../Core/Utils/DateUtils";
import { ExternalGLoader } from "../../../../Core/Layer/ExternalGLoader";
import ArrayUtil from "../../../../Core/Utils/ArrayUtil";
import { loadavg } from "os";
import Log from "../../../../Core/Log/Log";

export class GameLogWindow extends AbstractUIWindow {
    private list: fgui.GList;
    private infoArray: support.IGameLogData[] = [];
    protected getResList(): Array<string> {
        return [`Rank`];
    }
    protected onInit(): void {
        this._view = UICore.createObject("Rank", "GameLogWindow").asCom;
    }
    protected onShow(): void {
        EventMgr.on(`event_RespGameLogs`, this.event_RespGameLogs, this);
        this.list.itemRenderer = this.onlist.bind(this);
        GameModels.support.send_ReqGameLogs();
    }
    private event_RespGameLogs(info: support.IGameLogData[]): void {
        if (!info || info.length <= 0) {
            return;
        }
        this.infoArray = info;
        ArrayUtil.sortOn<support.IGameLogData>(this.infoArray, `Time`,true);
        this.list.numItems = this.infoArray.length;
    }
    private onlist(index: number, item: fgui.GComponent): void {
        const info = this.infoArray[index];
        Log.ins.log(1, info);
        item.getChild("sign").text = DateUtils.ins.formatTime(info.Time * 1000, 2);
        item.getChild("round").text = `回合数:${info.RoundCount}`;
        item.getChild(`titleTxt`).text = info.RoomType === 1 ? `随机匹配` : `房间对战`;
        if (info.WinSide === 0) {
            item.getChild("lwin").text = "胜利";
            item.getChild("rwin").text = "失败";
        } else {
            item.getChild("lwin").text = "失败";
            item.getChild("rwin").text = "胜利";
        }
        (item.getChild("lheadUrl") as ExternalGLoader).url = info.LAvatar ? info.LAvatar : UICore.getURL(`Common`, `icon`);
        (item.getChild("rheadUrl") as ExternalGLoader).url = info.RAvatar ? info.RAvatar : UICore.getURL(`Common`, `icon`);
        item.getChild("lnickName").text = info.LNick;
        item.getChild("rnickName").text = info.RNick;

        item.getChild("lkill").text = `击落数:${info.LAtkCount}`;
        item.getChild("rkill").text = `击落数:${info.RAtkCount}`;

        item.getChild("lindex").visible = info.FirstTurn === 0;
        item.getChild("rindex").visible = info.FirstTurn === 1;
    }
}